﻿using Newtonsoft.Json;
using System.IO;
using System.Windows;

namespace CSharpAllDemo.Echart
{
    /// <summary>
    /// EchartWindow.xaml 的交互逻辑
    /// </summary>
    public partial class EchartWindow : Window
    {
        public EchartWindow()
        {
            InitializeComponent();
            string htmlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Echart/echart.html");
            WebView2.Source = new Uri(htmlPath);



        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            /**
             *    ['2019-10-10', 200],
                        ['2019-10-11', 560],
                        ['2019-10-12', 750],
                        ['2019-10-13', 580],
                        ['2019-10-14', 250],
                        ['2019-10-15', 300],
                        ['2019-10-16', 450],
                        ['2019-10-17', 300],
                        ['2019-10-18', 100]
             */
            List<List<Object>> data = new List<List<Object>>();
            data.Add(new List<Object> { "2019-10-10", 200 });
            data.Add(new List<Object> { "2019-10-11", 560 });
            data.Add(new List<Object> { "2019-10-12", 750 });
            //var data = [ ['2019-10-10', 200],
            //            ['2019-10-11', 560],
            //            ['2019-10-12', 750],
            //            ['2019-10-13', 580],
            //            ['2019-10-14', 250],
            //            ['2019-10-15', 300],
            //            ['2019-10-16', 450],
            //            ['2019-10-17', 300],
            //            ['2019-10-18', 100]];
            string str = JsonConvert.SerializeObject(data);
            //MessageBox.Show(str);
            string script = $"refreshEchart({str});";

            WebView2.CoreWebView2.ExecuteScriptAsync(script);
        }
        private async Task<string> ExecuteScriptAsync(string cmd)
        {
            var str = await WebView2.CoreWebView2.ExecuteScriptAsync(cmd);
            return str;
        }

        private async void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            await WebView2.EnsureCoreWebView2Async();
            WebView2.CoreWebView2.WebMessageReceived += (sender, args) =>
            {
                var message = args.WebMessageAsJson;
                MessageBox.Show(message);
                // 处理来自 JavaScript 的消息



            };
        }
    }
}
